package com.bistu.xiong.tulingrobotchat.frags.main;


import android.view.View;
import android.widget.TextView;

import com.bistu.xiong.common.app.Fragment;
import com.bistu.xiong.common.widget.PortraitView;
import com.bistu.xiong.factory.model.db.User;
import com.bistu.xiong.factory.persistence.Account;
import com.bistu.xiong.tulingrobotchat.LaunchActivity;
import com.bistu.xiong.tulingrobotchat.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.ViewTarget;

import butterknife.BindView;
import butterknife.OnClick;

public class MeFragment extends Fragment {

    @BindView(R.id.appbar)
    View appBar;

    @BindView(R.id.portrait)
    PortraitView portraitView;

    @BindView(R.id.name)
    TextView name;

    @BindView(R.id.description)
    TextView description;

    public MeFragment() {
        // Required empty public constructor
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_main_me;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        //使用Glide加载图片给Appbar设置背景
        Glide.with(this)
                .load(R.drawable.bg_src_snow)
                .centerCrop()
                .into(new ViewTarget<View,GlideDrawable>(appBar) {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        this.view.setBackground(resource.getCurrent());
                    }
                });
        User me = Account.getCurrentUser();
        Glide.with(getContext())
                .load(me.getPortrait())
                .asBitmap()
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.drawable.default_portrait) //默认显示头像
                .into(portraitView);
        name.setText(me.getName());
        description.setText(me.getDesc());

    }

    @OnClick(R.id.logout)
    void Logout(){
        Account.destroyTokenAndUserId();
        LaunchActivity.show(getActivity());
        getActivity().finish();
    }
}
